package org.example.rabbitmq.latency;

import org.example.rabbitmq.config.LatencyProperties;
import org.example.rabbitmq.config.MultiQueueProperties;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class LatencyAutoConfig {
    @Autowired
    LatencyProperties latencyProperties;
    @Autowired
    MultiQueueProperties multiQueueProperties;

    @Bean
    public TopicExchange latencyExchange() {
        return new TopicExchange(latencyProperties.getExchangeName(), true, false);
    }

    @Bean
    public Queue latencyQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", multiQueueProperties.getExchangeName());
        arguments.put("x-message-ttl", latencyProperties.getLatency());
//        arguments.put("x-dead-letter-routing-key", "不设置");
        return new Queue(latencyProperties.getQueueName(), true, false, false, arguments);
    }

    @Bean
    public Binding latencyBinding() {
        return BindingBuilder.bind(latencyQueue()).to(latencyExchange()).with("#");
    }
}